package com.dc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dc.common.DateUtil;
import com.dc.common.ResponseResultVO;
import com.dc.entity.ro.ShiftManagementRO;
import com.dc.mapper.ShiftManagementMapper;
import com.dc.model.ShiftManagement;
import com.dc.service.ShiftManagementService;
import com.dc.util.RedisUtil;
import com.dc.util.SnowflakeIdWorker;
import com.dc.util.TokenUtil;
import com.dc.util.UserUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import static com.dc.common.ResponseResultVO.checkParameterIsNotNull;

@Service
public class ShiftManagementServiceImpl extends ServiceImpl<ShiftManagementMapper, ShiftManagement> implements ShiftManagementService {

    @Resource
    private ShiftManagementMapper shiftManagementMapper;

    @Resource
    private RedisUtil redisUtil;
    @Resource
    private TokenUtil tokenUtil;

    @Override
    public ResponseResultVO insertShiftManagement(ShiftManagement dto) {
        Long dutyUserId = dto.getDutyUserId();
        int i = 0;
        QueryWrapper<ShiftManagement> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(ShiftManagement::getDutyUserId, dutyUserId);
        queryWrapper.ge("create_time", DateUtil.getStartTime("start"));
        queryWrapper.lt("create_time", DateUtil.getStartTime("end"));
        queryWrapper.lambda().eq(ShiftManagement::getStatus, 0);
        ShiftManagement shiftManagement = shiftManagementMapper.selectOne(queryWrapper);
        if (null != shiftManagement) {
            //如果当日有正在值班的登录数据,那么不会再新增数据
            return null;
        } else {
            //如果没数据,
            dto.setId(SnowflakeIdWorker.generateId());
            i = shiftManagementMapper.insert(dto);
        }

        return ResponseResultVO.insertResult(i);
    }

    @Override
    public ResponseResultVO updateShiftManagement(ShiftManagementRO ro) {
        ShiftManagement shiftManagement = null;
        QueryWrapper<ShiftManagement> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(ShiftManagement::getDutyUserId, UserUtil.getUserId());
        Date currentDate = DateUtil.getDayBeginTime(new Date()); //当天时间0点
        queryWrapper.ge("create_time", DateUtil.getDateAddDay(currentDate, 0));
        queryWrapper.lt("create_time", DateUtil.getDateAddDay(currentDate, 1));
        queryWrapper.lambda().eq(ShiftManagement::getStatus, 0);
        shiftManagement = shiftManagementMapper.selectOne(queryWrapper);
        if (null == shiftManagement) {
            //说明是昨日登录，跨天中间没有重新登录过，那么就要取昨日最后一条数据。
            queryWrapper.clear();
            queryWrapper.lambda().eq(ShiftManagement::getDutyUserId, UserUtil.getUserId());
            queryWrapper.lambda().eq(ShiftManagement::getStatus, 0);
            queryWrapper.ge("create_time", DateUtil.getDateAddDay(currentDate, -1));
            queryWrapper.lt("create_time", DateUtil.getDateAddDay(currentDate, 0));
            shiftManagement = shiftManagementMapper.selectOne(queryWrapper);
        }
        shiftManagement.setHandoverUserName(ro.getHandoverUserName());
        shiftManagement.setHandoverUserId(ro.getHandoverUserId());
        shiftManagement.setHandoverContent(ro.getHandoverContent());
        shiftManagement.setStatus(1);
        shiftManagement.setHandoverTime(new Date());
        redisUtil.del("token:" + tokenUtil.getToken());
        return ResponseResultVO.updateResult(shiftManagementMapper.updateById(shiftManagement));
    }

    @Override
    public ResponseResultVO deleteShiftManagement(String ids) {
        checkParameterIsNotNull(ids, "id不能为空");
        List<String> idList = Arrays.asList(ids.split(","));
        return ResponseResultVO.deleteResult(shiftManagementMapper.deleteBatchIds(idList));
    }

    @Override
    public ResponseResultVO selectShiftManagementList(ShiftManagementRO ro) {
        Page page = new Page<Map<String, Object>>(ro.getCurrentPage(), ro.getPageSize());
        return ResponseResultVO.resultList(shiftManagementMapper.selectPage(page, new QueryWrapper()), ro);

    }
}
